using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace PacSnake
{
	/// <summary>
	/// We use this form to select a level set. We can start a the beginning of
	/// a level set, or we can continue a level set from the level where we
	/// left off the last time the game was played.
	/// </summary>
	public class FormLevelSet : System.Windows.Forms.Form
	{
	    // Holds the information about a level set
	    private LevelSetInfo levelSetInfo;
	    
	    // Holds a collection of LevelSetInfo objects
        private LevelSetInfoCollection levelSetInfos =
            new LevelSetInfoCollection();
		
		// Determines if we continue a level set or start from the beginning
		private bool continueLastPlayedLevel = true;
		
		// Checks if we pressed OK or cancel
		private bool okButtonPressed = false;
		

        #region Constructor
        
        /// <summary>
        /// Constructor: Loads the information of all the level sets we have
        /// and shows the titles of the level sets in the listbox.
        /// </summary>
		public FormLevelSet()
		{
			InitializeComponent();
			
			// Loads the information of all level sets
			levelSetInfos.LoadInfoLevelSets();
			
            // Adds the title of each level set to the listbox
            for (int i = 0; i < levelSetInfos.Count; i++)
                lstLevelSets.Items.Add(levelSetInfos[i].Title);
            
            lstLevelSets.SelectedIndex = 0;
		}
		
		#endregion
		
		#region Events for the listbox and buttons
		
        /// <summary>
        /// Update the level set information in the labels when the user
        /// selects a different level set.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstLevelSets_SelectedIndexChanged(object sender,
            System.EventArgs e)
        {
            int index = lstLevelSets.SelectedIndex;
            levelSetInfo = (LevelSetInfo)levelSetInfos[index];
            
            lblAuthor.Text = levelSetInfo.Author;
            lblEmail.Text = levelSetInfo.Email;
            lblUrl.Text = levelSetInfo.Url;
            txtDescription.Text = levelSetInfo.Description;
            lblNrOfLevels.Text = levelSetInfo.NrOfLevelsInSet.ToString();
        }
        
        
        /// <summary>
        /// Draws the level set titles in a particular color. Green means
        /// finished, orange means in progress, black is not yet started.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstLevelSets_DrawItem(object sender, DrawItemEventArgs e)
        {
            // Draw the background of the ListBox control for each item. Create
            // a new Brush and initialize to a Black colored brush by default.
            e.DrawBackground();
            Brush myBrush = Brushes.Black;

            // Determine the color of the brush to draw each item based on the
            // the value of StatusOfLevelSet.
            LevelSetInfo lsi = levelSetInfos[e.Index];
            if (lsi.LastFinishedLevelInSet == lsi.NrOfLevelsInSet)
                myBrush = Brushes.Green;
            else if (lsi.LastFinishedLevelInSet == 0)
                myBrush = Brushes.Black;
            else
                myBrush = Brushes.Orange;

            // Draw the current item text based on the current Font and the
            // custom brush settings.
            e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), 
                e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);

            // If the ListBox has focus, draw a focus rectangle around the
            // selected item.
            e.DrawFocusRectangle();
        }
        
        
        /// <summary>
        /// Set the name and filename of the level set
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, System.EventArgs e)
        {
            continueLastPlayedLevel = chkContinue.Checked ? true : false;
            
            okButtonPressed = true;
            this.Close();
        }


        /// <summary>
        /// Cancel pressed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
        
        #endregion
        
        #region Properties
        
        public LevelSetInfo LevelSetInfo
        {
            get { return levelSetInfo; }
        }
        
        public bool ContinueLastPlayedLevel
        {
            get { return continueLastPlayedLevel; }
        }
        
        public bool OkButtonPressed
        {
            get { return okButtonPressed; }
        }
        
        #endregion

		#region Windows Form Designer generated code
		
		private System.ComponentModel.Container components = null;
		
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox txtDescription;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ListBox lstLevelSets;
        private System.Windows.Forms.CheckBox chkContinue;
        private System.Windows.Forms.Label lblNrOfLevelsH;
        private System.Windows.Forms.Label lblNrOfLevels;
        private System.Windows.Forms.Label lblUrl;
        private System.Windows.Forms.Label lblEmail;
        private System.Windows.Forms.Label lblAuthor;
        private System.Windows.Forms.Label lblEmailH;
        private System.Windows.Forms.Label lblUrlH;
        private System.Windows.Forms.Label lblDescriptionH;
        private System.Windows.Forms.Label lblAuthorH;
        private System.Windows.Forms.Button btnOK;
        
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormLevelSet));
            this.btnCancel = new System.Windows.Forms.Button();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.txtDescription = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.lstLevelSets = new System.Windows.Forms.ListBox();
            this.chkContinue = new System.Windows.Forms.CheckBox();
            this.lblNrOfLevelsH = new System.Windows.Forms.Label();
            this.lblNrOfLevels = new System.Windows.Forms.Label();
            this.lblUrl = new System.Windows.Forms.Label();
            this.lblEmail = new System.Windows.Forms.Label();
            this.lblAuthor = new System.Windows.Forms.Label();
            this.lblEmailH = new System.Windows.Forms.Label();
            this.lblUrlH = new System.Windows.Forms.Label();
            this.lblDescriptionH = new System.Windows.Forms.Label();
            this.lblAuthorH = new System.Windows.Forms.Label();
            this.btnOK = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // btnCancel
            // 
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.btnCancel.Location = new System.Drawing.Point(248, 336);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.TabIndex = 22;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.White;
            this.panel1.Controls.Add(this.label1);
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(496, 56);
            this.panel1.TabIndex = 20;
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Arial Black", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label1.Location = new System.Drawing.Point(16, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(208, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Level set selection";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.label5);
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.txtDescription);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.lstLevelSets);
            this.groupBox1.Controls.Add(this.chkContinue);
            this.groupBox1.Controls.Add(this.lblNrOfLevelsH);
            this.groupBox1.Controls.Add(this.lblNrOfLevels);
            this.groupBox1.Controls.Add(this.lblUrl);
            this.groupBox1.Controls.Add(this.lblEmail);
            this.groupBox1.Controls.Add(this.lblAuthor);
            this.groupBox1.Controls.Add(this.lblEmailH);
            this.groupBox1.Controls.Add(this.lblUrlH);
            this.groupBox1.Controls.Add(this.lblDescriptionH);
            this.groupBox1.Controls.Add(this.lblAuthorH);
            this.groupBox1.Location = new System.Drawing.Point(8, 64);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(480, 256);
            this.groupBox1.TabIndex = 21;
            this.groupBox1.TabStop = false;
            // 
            // label5
            // 
            this.label5.ForeColor = System.Drawing.Color.Black;
            this.label5.Location = new System.Drawing.Point(132, 228);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(64, 16);
            this.label5.TabIndex = 19;
            this.label5.Text = "Not started";
            // 
            // label4
            // 
            this.label4.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(0)));
            this.label4.Location = new System.Drawing.Point(68, 228);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(68, 16);
            this.label4.TabIndex = 18;
            this.label4.Text = "In progress -";
            // 
            // label3
            // 
            this.label3.ForeColor = System.Drawing.Color.Green;
            this.label3.Location = new System.Drawing.Point(16, 228);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(56, 16);
            this.label3.TabIndex = 17;
            this.label3.Text = "Finished -";
            // 
            // txtDescription
            // 
            this.txtDescription.Enabled = false;
            this.txtDescription.Location = new System.Drawing.Point(208, 144);
            this.txtDescription.Multiline = true;
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.Size = new System.Drawing.Size(248, 72);
            this.txtDescription.TabIndex = 16;
            this.txtDescription.Text = "";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(16, 24);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(120, 16);
            this.label2.TabIndex = 1;
            this.label2.Text = "Available Level sets:";
            // 
            // lstLevelSets
            // 
            this.lstLevelSets.BackColor = System.Drawing.SystemColors.Window;
            this.lstLevelSets.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.lstLevelSets.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.lstLevelSets.Location = new System.Drawing.Point(16, 40);
            this.lstLevelSets.Name = "lstLevelSets";
            this.lstLevelSets.Size = new System.Drawing.Size(176, 186);
            this.lstLevelSets.TabIndex = 0;
            this.lstLevelSets.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.lstLevelSets_DrawItem);
            this.lstLevelSets.SelectedIndexChanged += new System.EventHandler(this.lstLevelSets_SelectedIndexChanged);
            // 
            // chkContinue
            // 
            this.chkContinue.Checked = true;
            this.chkContinue.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkContinue.Location = new System.Drawing.Point(208, 224);
            this.chkContinue.Name = "chkContinue";
            this.chkContinue.Size = new System.Drawing.Size(248, 16);
            this.chkContinue.TabIndex = 15;
            this.chkContinue.Text = "Continue with last played level in this set";
            // 
            // lblNrOfLevelsH
            // 
            this.lblNrOfLevelsH.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.lblNrOfLevelsH.Location = new System.Drawing.Point(208, 96);
            this.lblNrOfLevelsH.Name = "lblNrOfLevelsH";
            this.lblNrOfLevelsH.Size = new System.Drawing.Size(80, 16);
            this.lblNrOfLevelsH.TabIndex = 13;
            this.lblNrOfLevelsH.Text = "Level count:";
            // 
            // lblNrOfLevels
            // 
            this.lblNrOfLevels.Location = new System.Drawing.Point(288, 96);
            this.lblNrOfLevels.Name = "lblNrOfLevels";
            this.lblNrOfLevels.Size = new System.Drawing.Size(168, 16);
            this.lblNrOfLevels.TabIndex = 14;
            // 
            // lblUrl
            // 
            this.lblUrl.Location = new System.Drawing.Point(288, 80);
            this.lblUrl.Name = "lblUrl";
            this.lblUrl.Size = new System.Drawing.Size(168, 16);
            this.lblUrl.TabIndex = 10;
            // 
            // lblEmail
            // 
            this.lblEmail.Location = new System.Drawing.Point(288, 64);
            this.lblEmail.Name = "lblEmail";
            this.lblEmail.Size = new System.Drawing.Size(168, 16);
            this.lblEmail.TabIndex = 7;
            // 
            // lblAuthor
            // 
            this.lblAuthor.Location = new System.Drawing.Point(288, 48);
            this.lblAuthor.Name = "lblAuthor";
            this.lblAuthor.Size = new System.Drawing.Size(168, 16);
            this.lblAuthor.TabIndex = 6;
            // 
            // lblEmailH
            // 
            this.lblEmailH.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.lblEmailH.Location = new System.Drawing.Point(208, 64);
            this.lblEmailH.Name = "lblEmailH";
            this.lblEmailH.Size = new System.Drawing.Size(80, 16);
            this.lblEmailH.TabIndex = 5;
            this.lblEmailH.Text = "Email:";
            // 
            // lblUrlH
            // 
            this.lblUrlH.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.lblUrlH.Location = new System.Drawing.Point(208, 80);
            this.lblUrlH.Name = "lblUrlH";
            this.lblUrlH.Size = new System.Drawing.Size(80, 16);
            this.lblUrlH.TabIndex = 4;
            this.lblUrlH.Text = "URL:";
            // 
            // lblDescriptionH
            // 
            this.lblDescriptionH.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.lblDescriptionH.Location = new System.Drawing.Point(208, 128);
            this.lblDescriptionH.Name = "lblDescriptionH";
            this.lblDescriptionH.Size = new System.Drawing.Size(104, 16);
            this.lblDescriptionH.TabIndex = 3;
            this.lblDescriptionH.Text = "Description:";
            // 
            // lblAuthorH
            // 
            this.lblAuthorH.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.lblAuthorH.Location = new System.Drawing.Point(208, 48);
            this.lblAuthorH.Name = "lblAuthorH";
            this.lblAuthorH.Size = new System.Drawing.Size(80, 16);
            this.lblAuthorH.TabIndex = 1;
            this.lblAuthorH.Text = "Author:";
            // 
            // btnOK
            // 
            this.btnOK.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.btnOK.Location = new System.Drawing.Point(160, 336);
            this.btnOK.Name = "btnOK";
            this.btnOK.TabIndex = 19;
            this.btnOK.Text = "OK";
            this.btnOK.Click += new System.EventHandler(this.btnOk_Click);
            // 
            // FormLevelSet
            // 
            this.AcceptButton = this.btnOK;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(494, 369);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.btnOK);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FormLevelSet";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Choose Level Set";
            this.panel1.ResumeLayout(false);
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        
		#endregion
	}
}
